using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class AccountAoucherItem
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("AccountAItemID", "HLJXC_AccountAoucherItem");
		}
		public bool Exists(int AccountAItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_AccountAoucherItem");
			stringBuilder.Append(" where AccountAItemID=@AccountAItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAItemID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAItemID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.AccountAoucherItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_AccountAoucherItem(");
			stringBuilder.Append("AccountAoucherID,AAItemAccountSID,BorrowPrice,LoanPrice,AAItemSummary)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@AccountAoucherID,@AAItemAccountSID,@BorrowPrice,@LoanPrice,@AAItemSummary)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4),
				new SqlParameter("@AAItemAccountSID", SqlDbType.Int, 4),
				new SqlParameter("@BorrowPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@LoanPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@AAItemSummary", SqlDbType.NVarChar, 255)
			};
			array[0].Value = model.AccountAoucherID;
			array[1].Value = model.AAItemAccountSID;
			array[2].Value = model.BorrowPrice;
			array[3].Value = model.LoanPrice;
			array[4].Value = model.AAItemSummary;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public int AddTransaction(HKB.Data.Model.AccountAoucherItem model, Conn conn)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_AccountAoucherItem(");
			stringBuilder.Append("AccountAoucherID,AAItemAccountSID,BorrowPrice,LoanPrice,AAItemSummary)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@AccountAoucherID,@AAItemAccountSID,@BorrowPrice,@LoanPrice,@AAItemSummary)");
			stringBuilder.Append("  select SCOPE_IDENTITY()");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4),
				new SqlParameter("@AAItemAccountSID", SqlDbType.Int, 4),
				new SqlParameter("@BorrowPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@LoanPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@AAItemSummary", SqlDbType.NVarChar, 255)
			};
			array[0].Value = model.AccountAoucherID;
			array[1].Value = model.AAItemAccountSID;
			array[2].Value = model.BorrowPrice;
			array[3].Value = model.LoanPrice;
			array[4].Value = model.AAItemSummary;
			object obj = conn.ExecuteTransactionScalar(stringBuilder.ToString(), array);
			int result;
			if (obj == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(obj);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.AccountAoucherItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_AccountAoucherItem set ");
			stringBuilder.Append("AccountAoucherID=@AccountAoucherID,");
			stringBuilder.Append("AAItemAccountSID=@AAItemAccountSID,");
			stringBuilder.Append("BorrowPrice=@BorrowPrice,");
			stringBuilder.Append("LoanPrice=@LoanPrice,");
			stringBuilder.Append("AAItemSummary=@AAItemSummary");
			stringBuilder.Append(" where AccountAItemID=@AccountAItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4),
				new SqlParameter("@AAItemAccountSID", SqlDbType.Int, 4),
				new SqlParameter("@BorrowPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@LoanPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@AAItemSummary", SqlDbType.NVarChar, 255),
				new SqlParameter("@AccountAItemID", SqlDbType.Int, 4)
			};
			array[0].Value = model.AccountAoucherID;
			array[1].Value = model.AAItemAccountSID;
			array[2].Value = model.BorrowPrice;
			array[3].Value = model.LoanPrice;
			array[4].Value = model.AAItemSummary;
			array[5].Value = model.AccountAItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int AccountAItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountAoucherItem ");
			stringBuilder.Append(" where AccountAItemID=@AccountAItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAItemID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string AccountAItemIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountAoucherItem ");
			stringBuilder.Append(" where AccountAItemID in (" + AccountAItemIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.AccountAoucherItem GetModel(int AccountAItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLJXC_AccountAoucherItem ");
			stringBuilder.Append(" where AccountAItemID=@AccountAItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAItemID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAItemID;
			HKB.Data.Model.AccountAoucherItem accountAoucherItem = new HKB.Data.Model.AccountAoucherItem();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.AccountAoucherItem result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.AccountAoucherItem DataRowToModel(DataRow row)
		{
			HKB.Data.Model.AccountAoucherItem accountAoucherItem = new HKB.Data.Model.AccountAoucherItem();
			if (row != null)
			{
				if (row["AccountAItemID"] != null && row["AccountAItemID"].ToString() != "")
				{
					accountAoucherItem.AccountAItemID = int.Parse(row["AccountAItemID"].ToString());
				}
				if (row["AccountAoucherID"] != null && row["AccountAoucherID"].ToString() != "")
				{
					accountAoucherItem.AccountAoucherID = new int?(int.Parse(row["AccountAoucherID"].ToString()));
				}
				if (row["AAItemAccountSID"] != null && row["AAItemAccountSID"].ToString() != "")
				{
					accountAoucherItem.AAItemAccountSID = new int?(int.Parse(row["AAItemAccountSID"].ToString()));
				}
				if (row["BorrowPrice"] != null && row["BorrowPrice"].ToString() != "")
				{
					accountAoucherItem.BorrowPrice = new decimal?(decimal.Parse(row["BorrowPrice"].ToString()));
				}
				if (row["LoanPrice"] != null && row["LoanPrice"].ToString() != "")
				{
					accountAoucherItem.LoanPrice = new decimal?(decimal.Parse(row["LoanPrice"].ToString()));
				}
				if (row["AAItemSummary"] != null)
				{
					accountAoucherItem.AAItemSummary = row["AAItemSummary"].ToString();
				}
			}
			return accountAoucherItem;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HLJXC_AccountAoucherItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HLJXC_AccountAoucherItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_AccountAoucherItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.AccountAItemID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_AccountAoucherItem T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
